package com.example.eventbus;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {
        private TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=findViewById(R.id.text);
        Text text=new Text();
        inifunction();
        text.text();
    }

    private void inifunction() {
        //注册EventBus
        EventBus.getDefault().register(this);
    }
    /*注解的作用： EventBus用来识别*/
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getdata(BusData data)
    {
        text.setText(data.data);
        Log.d("xx",data.data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除注册EventBus 防止多次注册
        EventBus.getDefault().unregister(this);
    }
}